package org.grenobus;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import org.grenobus.BusSchedule.Minute;
import org.grenobus.BusSchedule.PerHourSchedule;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ScheduleActivity extends Activity {

	class TwoLinesAdapter extends BaseAdapter {

		BusSchedule schedule;
		
		public TwoLinesAdapter(BusSchedule s) {
			schedule = s;
		}
		
		public int getCount() {
			return schedule != null ? schedule.mSchedule.size() : 1;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View item = (View)LayoutInflater.from(parent.getContext()).inflate(R.layout.schedule_item, null, false);

			TextView t = (TextView)item.findViewById(R.id.text1);
			//t.setBackgroundResource(R.drawable.bg);
			if (schedule != null) {
				t.setText(Integer.toString(schedule.mSchedule.get(position).hour) + " h");
			} 
						
			t = (TextView)item.findViewById(R.id.text2); 
			if (schedule != null) {
				String desc = "";
				for (Minute m: schedule.mSchedule.get(position).minutes) {
					if (m.minute < 10)
						desc += '0';
					desc += m.minute;
					if (m.note != 0)
						desc += "<sup><small>" + m.note + "</small></sup>";
					desc += "  ";
				}

				t.setText(Html.fromHtml(desc));
			}
			
			return item;
		}
		
	};
	
	ListView mList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schedule_activity);

		BusSchedule schedule = new BusSchedule(LinesView.fPageHoraires.getHtml());
		
		String s[] = new String[schedule.mSchedule.size()];
		int i = 0;
		
		for (PerHourSchedule h : schedule.mSchedule) {
			String desc = "" + h.hour;
			for (Minute m: h.minutes) {
				desc += "\n" + m.minute;
				if (m.note != 0)
					desc += " (" + m.note + ")";
			}
			s[i++] = desc;
		}
		
		mList = (ListView) findViewById(R.id.ListSchedule);
		mList.setAdapter(new TwoLinesAdapter(schedule));
		
        final Button direction = (Button)findViewById(R.id.DirectionButton);
        direction.setText(schedule.mDirection);

        direction.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				LinesView.fPageHoraires = LinesView.fPageHoraires.postForm(null, 0);
				BusSchedule schedule = new BusSchedule(LinesView.fPageHoraires.getHtml());
				mList.setAdapter(new TwoLinesAdapter(schedule));
				direction.setText(schedule.getDirection());
			}
		});
        
        TextView date = (TextView)findViewById(R.id.TextDate);
        Calendar c = Calendar.getInstance();
		Date d = c.getTime();
        date.setText(DateFormat.getDateInstance().format(d));
    }

}